<template>
  <div  style="width:300px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="时间" required>
        <el-form-item prop="time">
          <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.time" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form-item>


      <el-form-item label="类别" prop="typeId">
        <el-input v-model="ruleForm.typeId"></el-input>
      </el-form-item>

      <el-form-item label="方式" prop="wayId">
        <el-input v-model="ruleForm.wayId"></el-input>
      </el-form-item>

      <el-form-item label="金额" prop="incomeMoney">
        <el-input v-model="ruleForm.incomeMoney"></el-input>
      </el-form-item>

      <el-form-item label="用户" prop="userId">
        <el-input v-model="ruleForm.userId"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>


    </el-form>
  </div>
</template>

<script>
  export default {
    name: "AdminUpdateExpend",
    data() {
      return {
        ruleForm: {
          iid: '',
          time: '',
          incomeMoney: '',
          userId: '',
          typeId: '',
          wayId: '',
        },
        rules: {
          wayId: [
            {required: true, message: '请输入类别', trigger: 'change'}
          ],
          typeId: [
            {required: true, message: '请输入方式', trigger: 'change'}
          ],
          userId: [
            {required: true, message: '请输入用户id', trigger: 'change'}
          ],
          time: [
            {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
          ],
          incomeMoney: [
            {required: true, pattern: /^[+-]?[0-9]+(\.[0-9]{1,2})?$/, message: '数据应为整数或小数，保留2位小数', trigger: 'blur'}
          ]

        }
      };
    },methods: {
      submitForm(formName) {
        let _this=this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm);


            let para = {
              userId: this.ruleForm.userId,
              iid: this.ruleForm.iid,
              time: this.ruleForm.time,
              incomeMoney: this.ruleForm.incomeMoney,
              typeId: this.ruleForm.typeId,
              wayId: this.ruleForm.wayId,
            };
            console.log(para);
            _this.$axios.post("updIncome",para).then(resp => {
              if(resp.data === 'success'){
                _this.$alert('修改成功！', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/adminIn')
                  }
                })
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
      console.log(this.$route.query.data);
      this.ruleForm = this.$route.query.data;

      this.ruleForm.time = new Date(this.$route.query.data.time);
    }

  }
</script>

<style scoped>

</style>
